<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/27 0027
 * Time: 上午 10:56
 */


namespace Admin\Controller;
use Admin\Model\ArticeViewModel;
use Think\Model;
use Think\Controller;

use Think\Page;

class ArticeController extends Controller
{
    public $_local_id;
    public function __construct()
    {
        parent::__construct();
        $this->_local_id = session('userInfo')['local_id'];
    }

    /**
     *  显示 文章列表
     */
    public function index()
    {
        $search_field = I('param.search_field');
        $search = trim(I('param.search'));
        $where[] = ' 1=1 ';
        $where[] = array('local_id' => session('userInfo')['local_id']);
        if ($search && $search_field) {
            $where[] = $search_field." like '%".$search."%'";
            $this->assign('search',$search);
            $this->assign('search_field',$search_field);
        }
        $where['local_id']=$this->_local_id;
        $count = M('b2c_artice')->where($where)->count();
        $Page = new Page($count,5);
        $page =$Page->show();
        $row = M('b2c_artice')->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($row as &$v)
        {
            $v['url'] = 'http://'.$_SERVER['HTTP_HOST'].__CONTROLLER__.'/view/id/'.$v['id'].'.html';
        }
        $this->assign(
            ['list'=>$row,
              'page'  =>$page
            ]
        );
        $this->display();
    }


    /**
     * 添加文章
     */

    public function add()
    {
        if (!IS_POST) {
            $where['id'] = I('id');
            $where['local_id']=$this->_local_id;
            $row =  M('b2c_artice')->where($where)->find();
            $row['content'] = htmlspecialchars_decode($row['content']);
            $this->assign('info',$row);
            $this->display('add');
        }else{
            $id = I('post.id');
            if (!$id) {
                if (empty($_POST['title']&&$_POST['content'])) {
                    $this->error('参数不能为空');
                    exit;
                }
                $data['title'] = I('post.title');
                $data['content'] = htmlspecialchars(I('post.content')) ;
                $data['local_id'] = $this->_local_id;
                if (M('b2c_artice')->add($data)) {
                    redirect(U('index'));
                }
                $this->error('添加失败');
                exit;
            }
            if (empty($_POST['title']&&$_POST['content'])) {
                $this->error('参数不能为空');
                exit;
            }
            $data['id']=$id;
            $data['title'] = I('post.title');
            $data['content'] = htmlspecialchars(I('post.content')) ;
            $data['local_id'] = $this->_local_id;
            if (M('b2c_artice')->save($data)) {
                redirect(U('index'));
            }
            $this->error('修改失败');

        }
    }

    /**
     * 删除文章
     */
    public function del()
    {

            if (!I('post.id')) {
                $this->error('参数错误');
                exit;
            }
            $id=I('post.id');
            $where['local_id'] = $this->_local_id;
            if (M('b2c_artice')->where($where)->delete($id)) {
                 echo json_encode(['msg'=>1,'message'=>'删除成功!']);
            }
        echo json_encode(['msg'=>0,'message'=>"删除失败!"]);
    }


    /**
     * 模板预览
     */
    public function view()
    {
        $template_id = I('get.id');


        if (!$template_id) {
            $this->error('缺少参数');
        }
        //后台预览不需要走微信的分享授权
        /*$jssdk = new JssdkEvent();
        $signPackage = $jssdk->GetSignPackage();
        $this->assign('signPackage', $signPackage);*/
        $Articeview  = new ArticeViewModel();
        $Articeview->template_id = $template_id;
        $data = $Articeview->viewTemplate();

        $this->assign($data);
        $this->display('view');
    }


}